package com.codingclub.application.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.codingclub.application.convert.SubjectAnswerDTOConverter;
import com.codingclub.application.convert.SubjectInfoDTOConverter;
import com.codingclub.application.dto.SubjectAnswerDTO;
import com.codingclub.application.dto.SubjectInfoDTO;
import com.codingclub.common.entity.PageResult;
import com.codingclub.common.entity.Result;
import com.codingclub.domain.bo.SubjectAnswerBO;
import com.codingclub.domain.bo.SubjectInfoBO;
import com.codingclub.domain.service.SubjectCategoryDomainService;
import com.codingclub.domain.service.SubjectInfoDomainService;
import com.codingclub.infra.basic.entity.SubjectInfo;
import com.codingclub.infra.basic.service.SubjectInfoService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 题目信息表(SubjectInfo)表控制层
 *
 * @author makejava
 * @since 2024-11-13 20:08:47
 */
@RestController
@RequestMapping("subject")
public class SubjectInfoController {
    /**
     * 服务对象
     */
    @Resource
    private SubjectInfoService subjectInfoService;

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 新增数据
     *
     * @param dto
     * @return 新增结果
     */
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO dto) {
        dto.validatorParam(dto,true);
        SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(dto);
        List<SubjectAnswerDTO> optionList = dto.getOptionList();
        if(optionList!=null&&!optionList.isEmpty()){
            List<SubjectAnswerBO> subjectAnswerBOS = SubjectAnswerDTOConverter.INSTANCE.convertDtoToBoList(optionList);
            subjectInfoBO.setAnswerBOS(subjectAnswerBOS);
            subjectInfoBO.setCategoryIds(dto.getCategoryIds());
            subjectInfoBO.setLabelIds(dto.getLabelIds());
        }
        boolean r=subjectInfoDomainService.add(subjectInfoBO);
        return Result.ok(r);
    }


    /**
     * 新增数据
     *
     * @param dto
     * @return 新增结果
     */
    @PostMapping("getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO dto) {
        dto.validatorQueryPage(dto);
        SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(dto);
        PageResult<SubjectInfoBO>pageResult=subjectInfoDomainService.getSubjectPage(subjectInfoBO);
        return Result.ok(pageResult);
    }

}

